package com.it.tencent.usercenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.it.tencent.usercenter.mapper.UserMapper;
import com.it.tencent.usercenter.model.domain.User;
import com.it.tencent.usercenter.service.UserService;

import com.it.tencent.usercenter.utils.AccountUtils;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.AccessibleObject;

/**
* @author 19360
* @description 针对表【existingUser】的数据库操作Service实现
* @createDate 2024-10-13 23:39:06
*/
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService {

    final String SALT = "xzc";
  public  final static String LOGIN_STATE = "login_state";

    @Resource
    UserMapper userMapper;
    /**
     * 账号注册
     * @return 用户id
     */

    @Override
    public long Register(User existingUser) {
        String userAccount = existingUser.getAccount();
        String userPassword = existingUser.getPassword();
        String checkPassword = existingUser.getCheckPassword();
        //1.检测是否为空
        if(StringUtils.isBlank(userAccount) || StringUtils.isBlank(userPassword) || StringUtils.isBlank(checkPassword)){
            return -1;
        }
//        账号长度不能小于4
        if(userAccount.length()<4){
            return -1;
        }
//        密码长度不能小于6
        if(userPassword.length()<6){
            return -1;
        }
//        3.正则表达式-》账号不能出现一些特殊字符
        boolean validAccount = AccountUtils.isValidAccount(userAccount);
        if(!validAccount){
            return -1;
        }

//        4.校验密码相同与否
        if(!StringUtils.equals(userPassword, checkPassword)){
            return -1;
        }

//        5.对密码进行加密

        String newPassword = AccountUtils.encryptMD5(userPassword+SALT);

//        6.账户不能重复

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", userAccount);
        User existingUse = this.getOne(queryWrapper);
        if(existingUser!=null){
            return -1;
        }

//        7.插入数据
        existingUser.setAccount(userAccount);
        existingUser.setPassword(newPassword);
        boolean isTrue = this.save(existingUser);
        if(!isTrue){
            return -1;
        }
        return existingUser.getId();
    }

    /**
     * 用户登录
     * @param user
     * @param request
     * @return
     */
    @Override
    public User Login(User user, HttpServletRequest request) {
//        1.和之前注册有点像
        String userAccount = user.getAccount();
        String userPassword = user.getPassword();
        //1.检测是否为空
        if(StringUtils.isBlank(userAccount) || StringUtils.isBlank(userPassword) ){
            return null;
        }
//        账号长度不能小于4
        if(userAccount.length()<4){
            return null;
        }
//        密码长度不能小于6
        if(userPassword.length()<6){
            return null;
        }
//        3.正则表达式-》账号不能出现一些特殊字符
        boolean validAccount = AccountUtils.isValidAccount(userAccount);
        if(!validAccount){
            return null;
        }
//        4.对密码进行加密

        String newPassword = AccountUtils.encryptMD5(userPassword+SALT);

//        5.检测是否有此账号
      QueryWrapper<User> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("account", userAccount);
      queryWrapper.eq("password", newPassword);
      User existingUser = this.getOne(queryWrapper);
      if(existingUser==null){
          log.info("账号或密码错误");
          return null;
      }
//      6.用户脱敏
        User safetyUser = safetyUser(user);
        //7. 记录用户的登录态，这个是存储在后端
        request.getSession().setAttribute(LOGIN_STATE,safetyUser);

      return safetyUser;


    }

    /**
     * 用户脱敏
     * @param existingUser
     * @return User
     */
    @Override
    public User safetyUser(User existingUser){
        User safetyUser = new User();
        safetyUser.setId(existingUser.getId());
        safetyUser.setName(existingUser.getName());
        safetyUser.setPhone(existingUser.getPhone());
        safetyUser.setAvatarUrl(existingUser.getAvatarUrl());
        safetyUser.setPassword(existingUser.getPassword());
        safetyUser.setAge(existingUser.getAge());
        safetyUser.setEmail(existingUser.getEmail());
        safetyUser.setGender(existingUser.getGender());
        safetyUser.setIsDelete(existingUser.getIsDelete());
        safetyUser.setAccount(existingUser.getAccount());
        safetyUser.setUserRole(existingUser.getUserRole());
        return safetyUser;

    }
}




